/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lector;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author nexo__000
 */
public class Lector2 {

    static String ficha;

    public static Document getDOMDoc(String file) {
        Document doc = null;
        try {
            File xmlFile = new File(file);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            doc = dBuilder.parse(xmlFile);
        } catch (Exception ex) {
            Logger.getLogger(Lector2.class.getName()).log(Level.SEVERE, null, ex);
        }
        return doc;
    }

    public static NodeList giveNodeList(Document doc) {
        NodeList nodeList = doc.getChildNodes();
        return nodeList;
    }

    public static void printNodeList(NodeList nodeList) {
        for (int count = 0; count < nodeList.getLength(); count++) {
            Node tempNode = nodeList.item(count);
            // make sure it's element node.
            if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
                // get node name and value
                System.out.print("<" + tempNode.getNodeName() + ">");
                // <editor-fold defaultstate="collapsed" desc=" ${Atributos} ">
//                if (tempNode.hasAttributes()) {
//                    // get attributes names and values
//                    NamedNodeMap nodeMap = tempNode.getAttributes();
//                    for (int i = 0; i < nodeMap.getLength(); i++) {
//                        Node node = nodeMap.item(i);
//                        System.out.println("attr name : " + node.getNodeName());
//                        System.out.println("attr value : " + node.getNodeValue());
//                    }
//                }
                // </editor-fold>
                if (tempNode.hasChildNodes()) {
                    // loop again if has child nodes
                    printNodeList(tempNode.getChildNodes());
                }
                System.out.print("</" + tempNode.getNodeName() + ">");
            } else {
                System.out.print(tempNode.getTextContent());
            }
        }
    }

}
